package com.hs.sentinelr.controller;

import cn.hutool.core.util.RandomUtil;
import com.hs.sentinelr.entity.StringCom;
import org.redisson.api.RedissonClient;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 测试
 *
 * @Author lingnan
 * @Date 2021/3/26 7:59 下午
 **/
@RequestMapping("sentinel")
@RestController
public class SentinelRTestController {

    @Resource
    private RedisTemplate redisTemplate;

    private static final String KEY = "dict::";


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedissonClient redisson;


    @GetMapping("/put/{key}/{value}")
    public String putKey(@PathVariable("key") String key, @PathVariable("value") String value) {
        StringCom stringCom = new StringCom().setKey(key).setValue(value);
        redisTemplate.boundValueOps(key).set(value);
        return stringCom.toString() + "  :  " + redisTemplate.hasKey(key);
    }

    @GetMapping("/putstr/{key}/{value}")
    public String putStrKey(@PathVariable("key") String key, @PathVariable("value") String value) {
        StringCom stringCom = new StringCom().setKey(key).setValue(value);
        stringRedisTemplate.opsForValue().set(key, value);
        return stringCom.toString();
    }

    @GetMapping("/get/{key}")
    public String getkey(@PathVariable("key") String key) {
        String value = (String) redisTemplate.boundValueOps(key).get();
        StringCom stringCom = new StringCom().setKey(key).setValue(value);
        return stringCom.toString();
    }

    @GetMapping("/getstr/{key}")
    public String getStrKey(@PathVariable("key") String key) {
        String s = stringRedisTemplate.boundValueOps(key).get();
        return new StringCom().setValue(s).setKey(key).toString();
    }

    @GetMapping("/putobj/{key}/{value}")
    public String putObj(@PathVariable("key") String key, @PathVariable("value") String value) {
        StringCom stringCom = new StringCom().setKey(key).setValue(value);
        redisTemplate.boundValueOps(key).set(stringCom);
        return stringCom.toString() + "  :  " + redisTemplate.hasKey(key);
    }

    @GetMapping("putHash")
    public String putHash() {
        for (int i = 0; i < 20; i++) {
            redisTemplate.boundHashOps(KEY + "m" + i).putAll(getData());
            redisTemplate.boundValueOps(KEY + "m" + i).expire(30, TimeUnit.MINUTES);
        }
        return "SUCCESS";
    }

    @GetMapping("getHash/{str}")
    public String getHash(@PathVariable("str") String str) {
        String value1 = (String) redisTemplate.boundHashOps(KEY + "m" + 5).get(str);
        return value1;
    }

    @GetMapping("getBatchHash")
    public String getBatchHash() {
        List list = redisTemplate.executePipelined((RedisCallback<Map>) connection -> {
            for (int i = 0; i < 10; i++) {
                connection.hGet((KEY + "m222" + i).getBytes(), ("kk" + i).getBytes());
            }
            return null;
        });
        list.stream()
                .forEach(c -> System.out.println(c));
        return list.toString();
    }

    public Map getData() {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 20; i++) {
            map.put("kk" + i, RandomUtil.randomString(80));
        }
        return map;
    }


}
